package zho.remotecontrol;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import zho.remotecontrol.gson.ElecApp;
import zho.remotecontrol.gson.Surroundings;

/*
架构图
数据通信
app智能插座控制端
实现：
1.充电后自动断电
WiFi插座由串口WiFi模块、继电器控制电路、继电器和输出触点构成
1.用户管理：
1.1用户登录，对上传的数据进行分析
1.2新用户注册
1.3用户信息维护，对原用户进行密码修改等
1.4退出当前用户

2场景管理
2.1 每个用户有其场景，同一用户场景不能重名并添加对应的icon
2.2 在每一场景中添加在其中对应的wifi插座并显示
2.3 支持在场景中添加/删除相应的wifi插座
2.4实现总开关

3实时状态
3.1 在wifi插座实时状态进行显示
3.2 实现对其状态的控制如开关
3.3 使用图片功能实现wifi插座的信息：如有电流，电压，功率，所用的电量

4连接模式
4.1.支持手机对wifi插座的直连功能，及远程连接的功能
4.2.直连模式：使得手机app自动会扫描在其附近的wifi插座并进行连接，进行数据的发送
4.3.远程连接：使用手机的网络的功能通过网络接连到wifi插座，实现查看

5.设置
5.1网络配置，配置当前网络名称
5.2图片加载，在wifi下载，移动网络下使用本地图片
5.3app上有清除缓存文件的功能

6.双击退出界面


论文结构

 */
public class MainActivity extends AppCompatActivity  {
    private TextView tv_content, tv_send_text;
    private EditText send_Text;
    private Button bt_send;

    private Toolbar toolbar;
    //布局控件
    private DrawerLayout drawerLayout;
    private SwipeRefreshLayout refreshLayout;

    //用电器对应的数组
    private ElecApp[] elecApps={
//        new ElecApp("用电器1",R.drawable.socket),
//        new ElecApp("用电器2",R.drawable.socket),
//        new ElecApp("用电器3",R.drawable.socket)
    };

    //场景对应的数组
    private Surroundings[] surroundingses={
        new Surroundings("场景1",R.drawable.s2),
        new Surroundings("场景2",R.drawable.s2),
        new Surroundings("场景3",R.drawable.s2)
    };
    //用电器和场景所对应的数据列表和适配器
    public static  List<ElecApp> elecAppList;
    public static List<Surroundings> surroundingsList;
    public static ElecAppAdapter elecAppAdapter;
    public static SurroundAdapter surroundAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        NavigationView navigationView = (NavigationView)findViewById(R.id.nav_view);
        toolbar = (Toolbar)findViewById(R.id.nav_toolbar);
        setSupportActionBar(toolbar);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                return false;
            }
        });


        initView();
        BindData();

        //在主界面上显示已添加的用电器
        RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_View);
        GridLayoutManager layoutManager = new GridLayoutManager(this,3);
        recyclerView.setLayoutManager(layoutManager);
        elecAppAdapter = new ElecAppAdapter(elecAppList);
        surroundAdapter = new SurroundAdapter(surroundingsList);
        recyclerView.setAdapter(elecAppAdapter);



        FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Snackbar.make(v,"Data deleted",Snackbar.LENGTH_SHORT)
//                        .setAction("Undo", new View.OnClickListener() {
//                            @Override
//                            public void onClick(View v) {
//                                Toast.makeText(MainActivity.this,"Data restored",Toast.LENGTH_SHORT).show();
//                            }
//                        }).show();
                Intent intent = new Intent(MainActivity.this,AddElecApp.class);
                MainActivity.this.startActivity(intent);
            }
        });


    }

    @Override
    protected void onResume() {

        super.onResume();
    }

    /**
    *实现双击退出程序
     */
    public boolean isfirst = true;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode){
            case KeyEvent.KEYCODE_BACK:
                if(isfirst){
                    isfirst = false;
                    Toast.makeText(this,"再按一次退出程序",Toast.LENGTH_SHORT).show();
                    new Timer().schedule(new TimerTask() {
                        @Override
                        public void run() {
                            isfirst = true;
                        }
                    },2000);
                    return true;
                }else{
                    this.finish();
                    System.exit(0);
                }
                break;
        }
        return super.onKeyDown(keyCode,event);

    }
    /**
     *初始化控件
     */
    private void initView(){
        elecAppList = new ArrayList<>();
        surroundingsList = new ArrayList<>();
    }
    /**
    *数据绑定
     */
    private void BindData(){
        elecAppList.clear();
        for(int i =0;i<elecApps.length;i++){
            elecAppList.add(elecApps[i]);
        }
        surroundingsList.clear();
        for(int i=0;i<surroundingses.length;i++){
            surroundingsList.add(surroundingses[i]);
        }
    }

}
